package com.xmjs.baselibrary.base.fragment.lib;

import android.app.Activity;
import android.view.View;

import androidx.annotation.IdRes;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.trello.rxlifecycle3.components.support.RxFragment;

import java.lang.reflect.Field;
import java.util.List;

public abstract class LibBaseFragment extends RxFragment {

    /**
     * 显示控件
     *
     * @param views
     */
    public void visible(View... views) {
        for (int i = 0; i < views.length; i++) {
            views[i].setVisibility(View.VISIBLE);
        }
    }

    /**
     * 隐藏控件
     *
     * @param views
     */
    public void invisible(View... views) {
        for (int i = 0; i < views.length; i++) {
            views[i].setVisibility(View.INVISIBLE);
        }
    }

    /**
     * 隐藏控件
     *
     * @param views
     */
    public void gone(View... views) {
        for (int i = 0; i < views.length; i++) {
            views[i].setVisibility(View.GONE);
        }
    }

    /**
     * 通过反射机制获取手机状态栏高度
     */
    public static int getStatusBarHeight(Activity activity) {
        Class<?> c = null;
        Object obj = null;
        Field field = null;
        int x = 0, statusBarHeight = 0;
        try {
            c = Class.forName("com.android.internal.R$dimen");
            obj = c.newInstance();
            field = c.getField("status_bar_height");
            x = Integer.parseInt(field.get(obj).toString());
            statusBarHeight = activity.getResources().getDimensionPixelSize(x);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return statusBarHeight;
    }

    /**
     * 添加 fragment
     *
     * @param idRes    容器ID
     * @param fragment 待添加的 fragment
     */
    public void addFragment(@IdRes int idRes, RxFragment fragment) {
        FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
        ft.add(idRes, fragment);
        ft.commit();
    }

    /**
     * 替换 fragment
     *
     * @param idRes    容器ID
     * @param fragment 待替换的 fragment
     */
    public void replaceFragment(@IdRes int idRes, RxFragment fragment) {
        FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
        ft.replace(idRes, fragment);
        ft.commit();
    }

    /**
     * 隐藏 fragment
     *
     * @param fragment
     */
    public void hideFragment(RxFragment fragment) {
        FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
        ft.hide(fragment);
        ft.commit();
    }

    /**
     * 显示 fragment
     *
     * @param fragment
     */
    public void showFragment(RxFragment fragment) {
        FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
        ft.show(fragment);
        ft.commit();
    }

    /**
     * 设置当前Fragment
     *
     * @param id
     * @param fragment
     */
    public void setCurrentFragment(int id, Fragment fragment) {
        List<Fragment> fragments = getActivity().getSupportFragmentManager().getFragments();
        FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
        if (!fragment.isAdded())
            fragmentTransaction.add(id, fragment, fragment.toString());
        for (int i = 0; i < fragments.size(); i++) {
            Fragment f = fragments.get(i);
            if (!f.getTag().equals(fragment.getId() + ""))
                fragmentTransaction.hide(f);
        }
        fragmentTransaction.show(fragment);
        fragmentTransaction.commitAllowingStateLoss();
        getActivity().getSupportFragmentManager().executePendingTransactions();
    }

}
